
/*
 * PowerOut class
 * This class represents restarting all events in Green house. Extends from Event
 * 
 * James Zhang
 * ID: 3069886
 * July 17, 2012
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Restart extends Event
{
    //Private string member containing path to file to read Events from
    private String filepath = "";

   /**
     * Default constructor
     *
     * @param g - Passed in the governing GreenhouseControls object reference
     * @param delayTime - Delay time for when to start this event
     * @param filename - Sets the path of the event file to read
     */
    public Restart(GreenhouseControls g, long delayTime, String filename)
    {
        super(g, delayTime);
        filepath = filename;
    }

    /**
     * Overridden run method reads the specified file using Scanner than starts the appropriate
     * Event.
     */
    public synchronized @Override
    void run()
    {
        //New file object from path of user inputed file
        File file = new File(filepath);
        try
        {
            //Scanner one
            Scanner scanner = new Scanner(file);

            //Delimits file line by line
            scanner.useDelimiter(System.getProperty("line.separator"));

            while (scanner.hasNext())
            {
                //Removes Event= and ,time= leaving on the event followed by the specified delay time
                String line = scanner.next();
                line = line.replace("Event=", "");
                line = line.replace(",time=", " ");

                //Scanner two
                Scanner lineScanner = new Scanner(line);

                while (lineScanner.hasNext())
                {
                    //Checks for Bell event. Bell is handled separately as they have a third argument for number of rings

                    //New String object for the Event name
                    String event = lineScanner.next();
                    if (event.equals("Bell"))
                    {
                        String inner = lineScanner.next();
                        inner = inner.replace(",rings=", " ");

                        //Scanner three (New scanner needed as there is an additional parameter, rings, to look for)
                        Scanner scan3 = new Scanner(inner);

                        //Integer object to hold the delay time using scanner three (scanner3)
                        int time;
                        time = scan3.nextInt();
                        int rings = 0;
                        while (scan3.hasNext())
                        {
                            //Sets the rings member
                            rings = scan3.nextInt();
                        }
                        //Adds new Bell event for each ring
                        for (int i = 0; i < rings; i++)
                        {
                            Bell bEvent = new Bell(gc, time + (i * 2000));
                            gc.eventList.add(bEvent);
                            gc.threadList.add(bEvent.thread);
                            bEvent.thread.start();
                        }                        
                        scan3.close();
                    } else
                    {
                        //Integer object to hold the delay time using scanner two (lineScanner)
                        int time = lineScanner.nextInt();

                        try
                        {
                            //Using reflection, creates a new Event given the Event name in String
                            Event myEvent = (Event) Class.forName(event).getConstructor(GreenhouseControls.class, long.class).newInstance(gc, time);
                            //Starts the event
                            gc.eventList.add(myEvent);
                            gc.threadList.add(myEvent.thread);
                            myEvent.thread.start();
                        } catch (Exception e)
                        {
                            System.out.println(e.toString());
                        }
                    }
                }

                lineScanner.close();
            }

            scanner.close();
        } catch (FileNotFoundException e)
        {
            System.out.println("Specified file is not found");
        }
    }

    /**
     * Override of toString()
     *
     * @return - Returns event in sentence form
     */
    public String toString()
    {
        return "Restarting system";
    }
}